package coop.bancocredicoop.proyectos.gd.reports;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import coop.bancocredicoop.proyectos.gd.domain.Customer;
import coop.bancocredicoop.proyectos.gd.domain.Document;
import coop.bancocredicoop.proyectos.gd.domain.DocumentType;

public class DocumentVersions {

	private List<Map<String, Object>> items;
	private HashMap<Long, CustomerFolders> customersFolders;
	private Integer totalItems;
	private Integer offset;
	
	public class CustomerFolders {
		
		public class DocumentTypeFolder {
			
			private List<Document> documents;
			private DocumentType documentType;
			
			public DocumentTypeFolder(DocumentType docType) {
				documentType = docType;
				documents = new ArrayList<Document>();
			}
			
			public DocumentType getDocumentType() {
				return documentType;
			}
			
			public void addDocument(Document document) {
				documents.add(document);
			}
			
			public List<Document> getDocuments() {
				return documents;
			}
			
		}
		
		private HashMap<Long, DocumentTypeFolder> documentTypeFolders;
		private Customer customer;
		
		public CustomerFolders(Customer customer) {
			this.customer = customer;
			documentTypeFolders = new HashMap<Long, DocumentTypeFolder>();
		}
		
		public Customer getCustomer() {
			return customer;
		}
		
		public void addDocument(Document document) {
			DocumentTypeFolder docTypeFolders = getOrCreateDocumentTypeFolder(document.getDocumentType());
			docTypeFolders.addDocument(document);
		}

		private DocumentTypeFolder getOrCreateDocumentTypeFolder(DocumentType documentType) {
			Long documentTypeKey = documentType.getId();
			if (documentTypeFolders.containsKey(documentTypeKey)) {
				return documentTypeFolders.get(documentTypeKey);			
			} else {
				DocumentTypeFolder documentTypeFolder = new DocumentTypeFolder(documentType);
				documentTypeFolders.put(documentTypeKey, documentTypeFolder);
				return documentTypeFolder;
			}
		}
		
		public List<DocumentTypeFolder> getDocumentTypeFolders() {			
			return new ArrayList<DocumentTypeFolder>(documentTypeFolders.values());			
		}
		
	}
	
	public DocumentVersions(List<Map<String, Object>> items, Integer totalItems, Integer offset) {
		this.items = items;
		this.offset = offset;
		this.totalItems = totalItems;
		customersFolders = new HashMap<Long, CustomerFolders>();
	}
	
	public List<Map<String, Object>> getItems() {
		return items;
	}
	
	public List<CustomerFolders> getCustomerFolders() {
		
		for (Map<String, Object> item : items) {
			addDocument((Document) item.get("document"));
		}
		
		return new ArrayList<CustomerFolders>(customersFolders.values());
		
	}
	
	private void addDocument(Document document) {
		CustomerFolders customerFolders = getOrCreateCustomerFolders(document);
		customerFolders.addDocument(document);
	}
	
	private CustomerFolders getOrCreateCustomerFolders(Document document) {
		Long customerKey = document.getCustomer().getIdPersona();
		if (customersFolders.containsKey(customerKey)) {
			return customersFolders.get(customerKey);			
		} else {
			CustomerFolders customerFolders = new CustomerFolders(document.getCustomer());
			customersFolders.put(customerKey, customerFolders);
			return customerFolders;
		}
	}

	public int getTotalItems() {
		return totalItems;
	}
	
	public int getOffset() {
		return offset;
	}
	
}
	

